#!/usr/bin/env bash
# db-create-view-as -- Creates a view with given SELECT query, coping with existing ones
# $ export CREATE_VIEW_TYPE=... CREATE_VIEW_EXTRA_CLAUSES=...
# $ export CREATE_IF_NOT_EXISTS=1
# $ export DROP_VIEW_TYPE=... DROP_TABLE_TYPE=...
# $ db-create-view-as VIEW SELECT_QUERY
##
set -euo pipefail
: ${CREATE_IF_NOT_EXISTS:=}
: ${CREATE_VIEW_TYPE:=} ${CREATE_VIEW_EXTRA_CLAUSES:=}
: ${DROP_VIEW_TYPE:=} ${DROP_TABLE_TYPE:=}

[[ $# -gt 0 ]] || usage "$0" "Missing VIEW name"
View=$1; shift
[[ $# -gt 0 ]] || usage "$0" "Missing SELECT_QUERY"
Query=$1; shift

viewDef=" AS $Query"

if db-execute '\d '"$View" &>/dev/null; then
    ! [[ -n $CREATE_IF_NOT_EXISTS ]] ||
        # exit if view or table with same name already exists
        exit 0
    # unconditionally drop existing table and view
    db-execute "DROP $DROP_VIEW_TYPE  VIEW  $View CASCADE" &>/dev/null || true
    db-execute "DROP $DROP_TABLE_TYPE TABLE $View CASCADE" &>/dev/null || true
fi

# unconditionally create a new view
exec db-execute "CREATE $CREATE_VIEW_TYPE VIEW $View $viewDef $CREATE_VIEW_EXTRA_CLAUSES"
